﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DotNetNuke.Services.Exceptions;
using DNNToolkit.Modules.CityTaxLocator.Components;
using DotNetNuke.Common;
using DotNetNuke.Entities.Modules;

namespace DNNToolkit.Modules.CityTaxLocator
{
    public partial class EditCityTaxLocator : PortalModuleBase
    {
        #region Privte Menbers
        private Int64 cityTaxID = 0;
        private Int64 CityTaxID
        {
            get
            {
                if (cityTaxID == 0 && Request.Params["CityTaxID"] != null)
                    cityTaxID = Convert.ToInt64(Request.Params["CityTaxID"]);
                return cityTaxID;
            }
        }
        #endregion

        #region Private Method
        /// <summary>
        /// Populates the city tax locator.
        /// </summary>
        private void PopulateCityTaxLocator()
        {
            CityTaxLocatorController controller = new CityTaxLocatorController();
            CityTaxInfo cityTaxInfo = new CityTaxInfo();
            cityTaxInfo = controller.GetCityTaxByID(CityTaxID);
            // the record exsits
            if (cityTaxInfo != null)
            {
                mStateTextBox.Text = cityTaxInfo.State;
                mLocalNameTextBox.Text = cityTaxInfo.LocalName;
                mLocRateTextBox.Text = cityTaxInfo.LocRate;
                mSchlRateTextBox.Text = cityTaxInfo.SchlRate;
                mWhRateTextBox.Text = cityTaxInfo.WhRate;
                mW2NameTextBox.Text = cityTaxInfo.W2Name;
                mLocalCDTextBox.Text = cityTaxInfo.LocalCD;
                mResIndTextBox.Text = cityTaxInfo.ResInd;
                mCountyNameTextBox.Text = cityTaxInfo.CountyName;
                mAdditionalJurTextBox.Text = cityTaxInfo.AdditionalJur;
                mCommentsTextBox.Text = cityTaxInfo.Comments;
            }
            else
            {
                Exception exc = new ArgumentException("The record you select has been deleted!");
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }
        #endregion

        #region Event Handlers
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    PopulateCityTaxLocator();
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// <summary>
        /// Handles the Click event of the cmdUpdate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            CityTaxInfo cityTaxInfo = new CityTaxInfo();
            cityTaxInfo.CityTaxID = CityTaxID;

            cityTaxInfo.State = mStateTextBox.Text;
            cityTaxInfo.LocalName = mLocalNameTextBox.Text;
            cityTaxInfo.LocRate = mLocRateTextBox.Text;
            cityTaxInfo.SchlRate = mSchlRateTextBox.Text;
            cityTaxInfo.WhRate = mWhRateTextBox.Text;
            cityTaxInfo.W2Name = mW2NameTextBox.Text;
            cityTaxInfo.LocalCD = mLocalCDTextBox.Text;
            cityTaxInfo.ResInd = mResIndTextBox.Text;
            cityTaxInfo.CountyName = mCountyNameTextBox.Text;
            cityTaxInfo.AdditionalJur = mAdditionalJurTextBox.Text;
            cityTaxInfo.Comments = mCommentsTextBox.Text;

            CityTaxLocatorController controller = new CityTaxLocatorController();
            controller.UpdateCityTaxInfo(cityTaxInfo);

            this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
        }

        /// <summary>
        /// Handles the Click event of the cmdDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void cmdDelete_Click(object sender, EventArgs e)
        {
            try
            {
                CityTaxLocatorController controller = new CityTaxLocatorController();
                controller.DeleteCityTaxByID(CityTaxID);

                this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
            }
            catch (Exception exc) //Delete failed
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// <summary>
        /// Handles the Click event of the cmdCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion
    }
}